export default {
    data() {
        return {
            list: [],
            reqParams: {
                page: 1,
                pageSize: 10
            },
            triggered: false,
            t:null, // 防抖定时器
            loadType: true, // 加载的类型  true 加载中 false 没有更多了
            loading: false, // 是否显示
            lowerFlag: false, // 是否到最后
			isOne:true // 是否是第一次加载
        }
    },
	computed:{
		firstRequest(){
			return !this.isOne && !this.list.length
		}  
	},
	
    mounted() {
        Object.assign(this.reqParams, this.params)
        this.init()
    },
    methods: {
        init() {
            if (this.lowerFlag) {
                return
            }
            this.loading = true
            this.$u.api[this.requestName] && this.$u.api[this.requestName](this.reqParams).then(res => {
				this.isOne = false
                if (!res.data.length) {
                    this.lowerFlag = true
                    this.loadType = false
                    setTimeout(() => {
                        this.loading = false
                    }, 5000)
                    return
                }
                if(res.data.length < this.reqParams.pageSize){
                    this.list.push(...res.data)
					this.$emit('get-list',this.list)
                    this.lowerFlag = true
                    this.loading = true
                    this.loadType = false
                    setTimeout(() => {
                        this.loading = false
                    }, 5000)
                    return
                }

                this.loading = false

                if (this.triggered) {

                    this.triggered = false
                    this.list = res.data
                } else {
                    this.list.push(...res.data)
                }
				this.$emit('get-list',this.list)
            })

        },
        upper() {
            if(this.t){
                return
            }

            this.triggered = true
            this.t = setTimeout(()=>{
                clearTimeout(this.t)
                this.t = null
            },500)
            this.reqParams.page = 1
            this.init()
        },
        lower() {
            this.reqParams.page++
            this.init()
        }
    }
}
